﻿using System;
using System.Collections.Generic;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class BbsController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public BbsController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void BbsTypeList()
        {
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("view", to(new QycmsController().Admin_Tab_List));
            var condition = " lng='" + DefaultLng + "'";
            DataPage<Bbs_TypeList> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var isclass = ctx.GetInt("isclass");
            switch (isclass)
            {
                case 1:
                    condition += " and isclass = 1 ";
                    break;

                case 2:
                    condition += " and isclass = 0 ";
                    break;
            }
            var purview = ctx.GetInt("purview");
            if (purview > 0)
            {
                condition += " and purview > 0 ";
            }

            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = Bbs_TypeList.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void BbsTypeAdd()
        {
            SetPublic();
            setpublic();
            target(BbsTypeAddDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", 1);
            set("tab", "true");
            var lng = DefaultLng;
            set("lng", lng);
            var memberpuvlist = Member_Class.findAll();
            bind("memberpuvlist", memberpuvlist);
            var tl = cdb.findAll<TimeList>();
            bind("timelist", tl);
        }

        [HttpPost]
        public virtual void BbsTypeAddDo()
        {
            var bt = new Bbs_TypeList();
            bt.pid = 50;
            bt.lng = ctx.Post("lng");
            bt.typename = ctx.Post("typename");
            bt.content = ctx.PostHtmlAll("content");
            bt.purview = ctx.PostInt("purview");
            bt.ispage = ctx.PostInt("ispage");
            bt.isclass = 1;
            bt.addtime = DateTime.Now;
            bt.pagemax = ctx.PostInt("pagemax");
            bt.listmax = ctx.PostInt("listmax");
            bt.isaddclass = ctx.PostInt("isaddclass");
            bt.ismail = ctx.PostInt("ismail");
            bt.putmail = ctx.Post("putmail");
            bt.mailcode = ctx.Post("mailcode");
            bt.ismenu = ctx.PostInt("ismenu");
            bt.isseccode = ctx.PostInt("isseccode");
            bt.inputtime = ctx.PostInt("inputtime");
            bt.iswap = ctx.PostInt("iswap");
            bt.istemplates = ctx.PostInt("istemplates");
            bt.templates = ctx.Post("templates");
            Result result = bt.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_add_log") + lang("log_extra_ok"), "{Id:" + bt.Id + ", Name:'" + bt.typename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void BbsTypeEdit(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(BbsTypeEditDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("type", "edit");
            Bbs_TypeList a = Bbs_TypeList.findById(Id);
            bind("read", a);
            var memberpuvlist = Member_Class.findAll();
            bind("memberpuvlist", memberpuvlist);
            var tl = cdb.findAll<TimeList>();
            bind("timelist", tl);
        }

        [HttpPost]
        public virtual void BbsTypeEditDo(Int64 Id)
        {
            var bt = Bbs_TypeList.findById(Id);
            bt.typename = ctx.Post("typename");
            bt.content = ctx.PostHtmlAll("content");
            bt.purview = ctx.PostInt("purview");
            bt.ispage = ctx.PostInt("ispage");
            bt.isclass = 1;
            bt.pagemax = ctx.PostInt("pagemax");
            bt.listmax = ctx.PostInt("listmax");
            bt.isaddclass = ctx.PostInt("isaddclass");
            bt.ismail = ctx.PostInt("ismail");
            bt.putmail = ctx.Post("putmail");
            bt.mailcode = ctx.Post("mailcode");
            bt.ismenu = ctx.PostInt("ismenu");
            bt.isseccode = ctx.PostInt("isseccode");
            bt.inputtime = ctx.PostInt("inputtime");
            bt.iswap = ctx.PostInt("iswap");
            bt.istemplates = ctx.PostInt("istemplates");
            bt.templates = ctx.Post("templates");
            Result result = bt.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_edit_log") + lang("log_extra_ok"), "{Id:" + bt.Id + ", Name:'" + bt.typename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void BbsTypeDel(Int64 Id)
        {
            Bbs_TypeList bt = Bbs_TypeList.findById(Id);
            if (bt.delete() > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_del_log") + lang("log_extra_ok"), "{Id:" + bt.Id + ", Name:'" + bt.typename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_del_log") + lang("log_extra_no"), "{Erros:''}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
        }

        [HttpPost]
        public virtual void BbsTypeSort()
        {
            var infoid = ctx.PostIdList("infoid");
            var pid = ctx.PostIdList("pid");
            var strArray = infoid.Split(new char[] { ',' });
            var strArray1 = pid.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                string condition = string.Format("Id = {0}", strArray[i]);
                db.updateBatch<Bbs_TypeList>("set pid=" + strArray1[i], condition);
            }
            logService.Add((User)ctx.viewer.obj, lang("forumtype_log_sort") + lang("log_extra_ok"), "{Id:" + infoid + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void BbsTypeSetting()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<Bbs_TypeList>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_log_istype") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forumtype_log_istype") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet, HttpPost]
        public virtual void BbsMainList()
        {
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("view", to(new QycmsController().Admin_Tab_List));
            var condition = " lng='" + DefaultLng + "'";
            DataPage<Bbs> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var isclass = ctx.GetInt("isclass");
            switch (isclass)
            {
                case 1:
                    condition += " and isclass = 1 ";
                    break;

                case 2:
                    condition += " and isclass = 0 ";
                    break;
            }
            var istop = ctx.GetInt("istop");
            switch (istop)
            {
                case 1:
                    condition += " and istop = 1 ";
                    break;

                case 2:
                    condition += " and istop = 0 ";
                    break;
            }
            var noreply = ctx.GetInt("noreply");
            switch (noreply)
            {
                case 1:
                    condition += " and noreply = 1 ";
                    break;

                case 2:
                    condition += " and noreply = 0 ";
                    break;
            }
            var btid = ctx.GetInt("btid");
            if (btid > 0)
            {
                condition += " and btid = " + btid;
            }
            var serchekey = ctx.Get("serchekey");
            if (!serchekey.StrIsNullOrEmpty())
            {
                condition += " and title like '%" + serchekey + "%'";
            }
            var upbid = ctx.GetInt("upbid");
            condition += " and upbid = " + upbid;

            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            //ctx.web.ResponseWrite(condition);
            //ctx.web.ResponseEnd();
            CurrentRequest.setCurrentPage(page_id);
            list = Bbs.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void BbsMainAdd(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(BbsMainAddDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", 1);
            set("tab", "true");
            var lng = DefaultLng;
            set("lng", lng);
            var b = Bbs.findById(Id);
            bind("read", b);
        }

        [HttpPost]
        public virtual void BbsMainAddDo(Int64 Id)
        {
            var b = Bbs.findById(Id);
            b.replynum++;
            b.update();
            b = new Bbs();
            b.btid = ctx.PostInt("btid");
            b.upbid = Id;
            b.adminid = ctx.viewer.obj.Id;
            b.userid = 0;
            b.lng = ctx.Post("lng");
            b.title = ctx.Post("title");
            b.content = ctx.PostHtmlAll("content");
            b.username = String.Empty;
            b.email = String.Empty;
            b.qq = String.Empty;
            b.msn = String.Empty;
            b.address = String.Empty;
            b.tel = String.Empty;
            b.mobile = String.Empty;
            b.replynum = 0;
            b.click = 0;
            b.addtime = DateTime.Now;
            b.retime = DateTime.Now;
            b.isclass = 1;
            b.istop = 0;
            b.ipadd = ctx.Ip;
            Result result = b.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_add_log") + lang("log_extra_ok"), "{Id:" + b.Id + ", Name:'" + b.title + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void BbsMainEdit(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(BbsTypeEditDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var b = Bbs.findById(Id);
            bind("read", b);

        }

        [HttpPost]
        public virtual void BbsMainEditDo(Int64 Id)
        {
            var b = Bbs.findById(Id);
            b.title = ctx.Post("title");
            b.content = ctx.Post("content");
            b.username = ctx.Post("username");
            b.email = ctx.Post("email");
            b.qq = ctx.Post("qq");
            b.msn = ctx.Post("msn");
            b.address = ctx.Post("address");
            b.tel = ctx.Post("tel");
            b.mobile = ctx.Post("mobile");
            Result result = b.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_edit_log") + lang("log_extra_ok"), "{Id:" + b.Id + ", Name:'" + b.title + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void BbsMainDel()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<Bbs>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_del_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_del_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void BbsMainSetting()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<Bbs>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_class_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("forummain_class_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet, HttpPost]
        public virtual void BbsMainSearch()
        {
            SetPublic();
            setpublic();
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var lnglist = Lng.find("isopen=:i order by pid asc,id desc").set("i", 1).list();
            bind("lnglist", lnglist);
            var bbstype = Bbs_TypeList.find(" lng=:l").set("l", DefaultLng).list();
            bind("bbstype", bbstype);
        }

    }
}
